﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using mediatheque.Areas.Admin.Models;

namespace mediatheque.Areas.Admin.Controllers
{
    public class CategoriesController : Controller
    {
        protected Oeuvres myOeuvre = new Oeuvres();
        //
        // GET: /Admin/Oeuvres/

        public ActionResult Index(FormCollection data)
        {
            if (data.Count > 1)
            {
                categories c = new categories
                {
                    lib = data["libelle"]
                };

                if (TryUpdateModel(c))
                {
                    myOeuvre.categories.AddObject(c);
                    myOeuvre.SaveChanges();
                }
            }
            return View(myOeuvre);
        }

        public ActionResult Delete(int id)
        {
            try
            {
                var c = myOeuvre.categories.First(i => i.id == id);
                myOeuvre.categories.DeleteObject(c);
                myOeuvre.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return RedirectToAction("Index");
            }
        }
    }
}
